package wawi.actions;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;

import wawi.db.DbConnector;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Lieferant extends ActionSupport
{
	private Integer recid;
	private String firma;
	private String adresse;
	private String telefon;
	private String mail;
	
	
	@SkipValidation
	public String neu()
	{		
		System.out.println("Neuer Lieferant");		
		return ActionSupport.INPUT;
	}
	
	
	public String speichern()
	{				
		if (recid == -1)
		{
			System.out.println("Write");
			if (DbConnector.getInstance().writeLieferant(this))			
				return SUCCESS;		
		}
		else
		{
			System.out.println("Update");
			if (DbConnector.getInstance().updateLieferant(this))
				return SUCCESS;
		}
		
		return ActionSupport.ERROR;
	}
	
	
	@SkipValidation
	public String bearbeiten()
	{
			
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		
		try
		{
			int recid = Integer.parseInt(request.getParameter("recid"));
			
			if (DbConnector.getInstance().readLieferant(recid, this))
				return ActionSupport.INPUT;		
		}
		catch (NumberFormatException nfe)
		{
			addActionError("Falsche REC-ID");
		}
		
		return ERROR;
	}
		
	
	
	
	public Lieferant()
	{
		super();
		
		recid = -1;
		firma = "";
		adresse = "";
		telefon = "";
		mail = "";
	}

	public Integer getRecid() {
		return recid;
	}

	public void setRecid(Integer recid) {
		this.recid = recid;
	}

	public String getFirma() {
		return firma;
	}

	public void setFirma(String firma) {
		this.firma = firma;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public String getTelefon() {
		return telefon;
	}

	public void setTelefon(String telefon) {
		this.telefon = telefon;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}
	
	
	
}
